﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using BCNPCObject.DB;
using DbLinq;
using DbLinq.Oracle;
using CMISLibrary.Entities;
using CMISLibrary;

namespace BCNPCObject.EXCEL
{
    public class cls_DD_BBAN_BDONG_Controller
    {
        #region   Atributes

        //private DataSet CMIS_Header = new CMISOutputParameter();
        private DD_BBAN_BDONG info;

        #endregion

        #region   Constructor

        public cls_DD_BBAN_BDONG_Controller()
        {
            info = new DD_BBAN_BDONG();
        }
        public cls_DD_BBAN_BDONG_Controller(DD_BBAN_BDONG Info)
        {
            this.info = Info;
        }

        #endregion

        #region   Properties

        public DD_BBAN_BDONG pInfor
        {
            get { return info; }
            set { info = value; }
        }
        #endregion

        #region Methods
        public bool update_hoso_cto(string ma_dviqly,string ma_cto,DateTime ngay_bdong)
        {
            try
            {
                var query = from ord in CMIS2.DB.DD_HOSO_CTO
                            where ord.MA_DVIQLY == ma_dviqly && ord.MA_CTO == ma_cto
                            select ord;

                foreach (DD_HOSO_CTO ord in query)
                {
                    ord.MA_BDONG = "B";
                    ord.NGAY_BDONG = ngay_bdong;
                }

                // Submit the changes to the database.
                CMIS2.DB.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                //strErr = ex.ToString();
                CMIS2.ResetDB();
                return false;
            }
        }
        public bool update_DD_HOSO_CTO(DataTable dt, ref string maloi)
        {
            try
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        var query = from p in CMIS2.DB.DD_HOSO_CTO
                                    where (p.MA_DVIQLY == dt.Rows[i]["MA_DVIQLY"].ToString() && p.MA_CTO == dt.Rows[i]["MA_CTO"].ToString())
                                    select p;
                        foreach (DD_HOSO_CTO ord in query)
                        {
                            ord.MA_BDONG = "A";
                           //ord.NGAY_BDONG = ngay_bdong;
                        }
                    }
                    return true;
                }
                else
                { return false; }
            }
            catch (Exception ex)
            {
                maloi = ex.ToString();
                CMIS2.ResetDB(); return false;
            }
        }
        public bool insert_DD_BBAN_BDONG(ref Int64 id_bb,ref string maloi)
        {
            long _ID = CMIS2.DB.ExecuteCommand("Select SEQ_DD_BBAN_BDONG.NEXTVAL from dual");
            if (_ID == -1) return false;
            try
            {
                info.ID_BBAN = Convert.ToInt64(_ID);
                DD_BBAN_BDONG objDD_BBAN_BDONG = new DD_BBAN_BDONG()
                {
                    MA_DVIQLY = info.MA_DVIQLY,
                    ID_BBAN = info.ID_BBAN,
                    MA_DDO = info.MA_DDO,
                    SO_BBAN = info.SO_BBAN,
                    NGAY_BDONG = info.NGAY_BDONG,
                    MA_LDO = info.MA_LDO,
                    MA_NVIEN = info.MA_NVIEN,
                    MA_NVKCHI = info.MA_NVKCHI,
                    NGAY_KCHI = info.NGAY_KCHI,
                    MA_CNANG = info.MA_CNANG,
                    NGAY_TAO = info.NGAY_TAO,
                    NGUOI_TAO = info.NGUOI_TAO,
                    NGAY_SUA = info.NGAY_SUA,
                    NGUOI_SUA = info.NGUOI_SUA
                };
                CMIS2.DB.DD_BBAN_BDONG.InsertOnSubmit(info);
                CMIS2.DB.SubmitChanges();
                id_bb = _ID;
                CMIS2.ResetDB();
                return true;
            }
            catch (Exception ex)
            {
                maloi = ex.ToString();
                CMIS2.ResetDB();
                return false;
            }
        }
        public bool delete_DD_BBAN_BDONG(DataTable dt, ref string maloi)
        {
            try
            {
                if (dt != null && dt.Rows.Count >0)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        var query = from p in CMIS2.DB.DD_BBAN_BDONG
                                    where (p.MA_DVIQLY == dt.Rows[i]["MA_DVIQLY"].ToString() && p.ID_BBAN == Convert.ToInt64(dt.Rows[i]["ID_BBAN"]))
                                    select p;
                        if (query != null && query.Count() > 0)
                        {
                            CMIS2.DB.DD_BBAN_BDONG.DeleteAllOnSubmit(query.ToList());
                            CMIS2.DB.SubmitChanges();
                        }
                    }
                    return true;
                }
                else
                { return false; }
            }
            catch (Exception ex)
            {
                maloi = ex.ToString();
                CMIS2.ResetDB(); return false;
            }
        }
        #endregion
    }
}
